<?php
/**
 * 支付相关
 * 不需要签名访问
 */

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Services\PayService;
use App\Traits\PayHelper;
use Illuminate\Http\Request;
use Pay;
use Log;

class PayController extends Controller
{
    use PayHelper;

    /**
     * 微信扫码支付
     * @return string|void
     */
    public function wechatPay()
    {
        $order = [
            'out_trade_no' => time(),
            'body'         => 'subject-测试',
            'total_fee'    => 1,
        ];
        $qr    = $this->wechatPayByScan($order['out_trade_no'], $order['total_fee'], $order['total_fee']);

        return $this->createQr($qr, 200);
    }

    /**
     * 支付宝app支付结果异步回调
     * @param Request $request
     * @return string
     */
    public function alipayNotify(Request $request)
    {
        Log::info('[支付宝回调通知] 入口', $request->all()); //debug

        $alipay = Pay::alipay();
        try {
            $data = $alipay->verify();

            Log::info('[支付宝回调通知]', $data->all());

            // 通知验证 && 生成shipping记录
            PayService::getInstance()->alipayCallbackDo($data);

        } catch (\Exception $e) {
            // 通知异常记录
            Log::debug($e->getMessage());
        }

        return $alipay->success();
    }

    /**
     * 支付成功后同步通知地址
     * @param Request $request
     */
    public function alipayReturn(Request $request)
    {
        Log::info('[支付宝同步通知] 入口', $request->all()); //debug

        $data = Pay::alipay()->verify();

        // 订单号：$data->out_trade_no
        // 支付宝交易号：$data->trade_no
        // 订单总金额：$data->total_amount
    }

    /**
     * 微信app支付结果异步回调
     * @param Request $request
     * @return string
     */
    public function wechatPayNotify(Request $request)
    {
        Log::info('[微信回调通知] 入口', $request->all()); //debug

        $wechat = Pay::wechat();
        try {
            $data = $wechat->verify();

            Log::debug('Wechat notify', $data->all());

            // 通知验证 && 生成shipping记录
            PayService::getInstance()->wechatPayCallbackDo();

        } catch (\Exception $e) {
            // 通知异常记录
            Log::debug($e->getMessage());
        }

        return $wechat->success();
    }
}
